// @ts-nocheck
import router from '@system.router';
import prompt from '@system.prompt';

export default {
    data: {
        item: [{
                   icon: "/common/icon/service.png",
                   name: "客服",
                   uri: ""
               },
               {
                   icon: "/common/icon/favor.png",
                   name: "收藏",
                   uri: ""
               },
               {
                   icon: "/common/icon/shop.png",
                   name: "购物车",
                   uri: "pages/trolley/trolley"
               },
        ],
        commodity: {
            itemId: 0,
            num: 1,
            itemName: "",
            itemImagePath: [],
            itemPrice: 0,
            itemDescription: ""
        },
        isAddToTrolley: true,
    },
    addNumChange(event) {
        this.commodity.num = event.value
    },
    onClickShop(i) {
        if (i == 2) {
            router.push({
                uri: this.item[i].uri,
                params: {
                    showBackBt: true
                }
            })
        } else {
            prompt.showToast({
                message: "正在努力开发中~"
            })
        }
    },
    onInit() {
        var data = router.getParams().commodity;
        this.commodity = {
            itemId: data.itemId,
            num: 1,
            itemName: data.itemName,
            itemImagePath: data.itemImagePath,
            itemPrice: data.itemPrice,
            itemDescription: data.itemDescription
        }
    },
    back() {
        router.back()
    },
    showNumDialog(addToTrolley) {
        this.isAddToTrolley = addToTrolley
        this.$element("commodity-num").show()
    },
    numConfirm() {
        if (this.isAddToTrolley) {
            this.addToTrolley()

        } else {
            this.dismissNumDialog()
            var goods = []
            goods.push(this.commodity)
            router.push({
                uri: "pages/orderCreate/orderCreate",
                params: {
                    goods: goods
                }
            })
        }
    },
    addToTrolley() {
        console.log("add commodity to trolley")
        var i = -1
        for (let j = 0; j < getApp().globalData.cartData.length; j++) {
            if (getApp().globalData.cartData[j].itemId == this.commodity.itemId) {
                i = j
                break
            }
        }

        if (i >= 0) {
            // 商品已在购物车中
            getApp().globalData.cartData[i].num += this.commodity.num
        } else {
            // 商品不在购物车中
            getApp().globalData.cartData.push({
                itemId: this.commodity.itemId,
                checked: true,
                num: this.commodity.num,
                itemName: this.commodity.itemName,
                itemPrice: this.commodity.itemPrice,
                itemDescription: this.commodity.itemDescription,
                itemImagePath: this.commodity.itemImagePath,
            })
        }
        // 添加到购物车之后
        this.dismissNumDialog()
        prompt.showToast({
            message: "添加成功~"
        })
    },
    dismissNumDialog() {
        this.$element("commodity-num").close()
    },
    numMinusOne() {
        this.commodity.num -= 1
    },
    numPlusOne() {
        this.commodity.num += 1
    },
    toGallery(index) {
        console.log("点击的下标" + index)
        router.push({
            uri: "pages/gallery/gallery",
            params: {
                itemImagePath: this.commodity.itemImagePath,
                currentIndex: index
            }
        })
    },
    onShow() {
        var message = router.getParams().message
        if (message != undefined) {
            prompt.showToast({
                message: message,
            })
        }
    },
}

